package com.jhy.javacase.io;

import org.junit.jupiter.api.Test;

import java.io.*;
import java.util.Scanner;


/**
 * @author jhy
 * @ClassName PrintWriterScannerCase.java
 * @Description 处理文本的输入输出
 * @createTime 2022年02月28日 10:48:00
 */
public class PrintWriterScannerCase {


    //1.使用PrintWriter(写出器)进行控制台打印
    @Test
    public void consoleLog(){
        //System.out为输出流PrintStream
        //true表示设置写出器为自动冲刷模式(冲刷缓冲区)
        PrintWriter printWriter = new PrintWriter(System.out,true);
        printWriter.println(1);

    }

    //2.使用PrintWriter进行文件写出
    @Test
    public void testPrintWriter() throws FileNotFoundException, UnsupportedEncodingException {
        PrintWriter printWriter = new PrintWriter("E:\\java项目\\Guava学习\\guava-case\\guava-case\\src\\main\\resources\\io\\targetFile.txt", "UTF-8");
        String name = "海阳";
        int age = 25;
        double salary = 10000000.1;
        printWriter.print("name: " + name);
        printWriter.print("age: " + age);
        printWriter.println("salary: " + salary);
        printWriter.close(); //必须关闭，否则文件中没有数据
    }

    //3.使用Scanner读取控制台的输入
    public static void main(String[] args) {
        //System.in是输入流InputStream
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()){
            System.out.println(sc.next());
        }
    }

    //4.使用Scanner读取文件输入
    @Test
    public void scannerFile() throws FileNotFoundException {
        Scanner sc = new Scanner(new File("E:\\java项目\\Guava学习\\guava-case\\guava-case\\src\\main\\resources\\io\\sourceFile.txt"));
        while (sc.hasNext()){
            String s = sc.nextLine();
            System.out.println(s);
        }
    }

}
